home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 March / PCWorld_2007-03_cd.bin / v cisle / httrack / httrack-3.41-2.exe / {app} / src / htsname.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-10-15  |  50.4 KB  |  1,485 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: httrack.c subroutines:                                 */
  34. /*       savename routine (compute output filename)             */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. /* Internal engine bytecode */
  39. #define HTS_INTERNAL_BYTECODE
  40.  
  41. #include "htscore.h"
  42. #include "htsname.h"
  43. #include "htsmd5.h"
  44. #include "htstools.h"
  45. #include <ctype.h>
  46.  
  47. #undef test_flush
  48. #define test_flush if (opt->flush) { fflush(opt->log); fflush(opt->log); }
  49.  
  50. #define ADD_STANDARD_PATH \
  51.     {  /* ajout nom */\
  52.       char BIGSTK buff[HTS_URLMAXSIZE*2];\
  53.       buff[0]='\0';\
  54.       strncatbuff(buff,start_pos,(int) (nom_pos - start_pos));\
  55.       url_savename_addstr(save,buff);\
  56.     }
  57.  
  58. #define ADD_STANDARD_NAME(shortname) \
  59.     {  /* ajout nom */\
  60.       char BIGSTK buff[HTS_URLMAXSIZE*2];\
  61.       standard_name(buff,dot_pos,nom_pos,fil_complete,(shortname));\
  62.       url_savename_addstr(save,buff);\
  63.     }
  64.  
  65.  
  66. /* Avoid stupid DOS system folders/file such as 'nul' */
  67. /* Based on linux/fs/umsdos/mangle.c */
  68. static const char *hts_tbdev[] =
  69. {
  70.     "/prn", "/con", "/aux", "/nul",
  71.     "/lpt1", "/lpt2", "/lpt3", "/lpt4",
  72.     "/com1", "/com2", "/com3", "/com4",
  73.     "/clock$",
  74.     "/emmxxxx0", "/xmsxxxx0", "/setverxx",
  75.     ""
  76. };
  77.  
  78.  
  79. #define URLSAVENAME_WAIT_FOR_AVAILABLE_SOCKET() do { \
  80.   int prev = opt->state._hts_in_html_parsing; \
  81.   while(back_pluggable_sockets_strict(sback, opt) <= 0) { \
  82.     opt->state. _hts_in_html_parsing = 6; \
  83.     /* Wait .. */ \
  84.     back_wait(sback,opt,cache,0); \
  85.     /* Transfer rate */ \
  86.     engine_stats(); \
  87.     /* Refresh various stats */ \
  88.     HTS_STAT.stat_nsocket=back_nsoc(sback); \
  89.     HTS_STAT.stat_errors=fspc(opt,NULL,"error"); \
  90.     HTS_STAT.stat_warnings=fspc(opt,NULL,"warning"); \
  91.     HTS_STAT.stat_infos=fspc(opt,NULL,"info"); \
  92.     HTS_STAT.nbk=backlinks_done(sback,liens,lien_tot,ptr); \
  93.     HTS_STAT.nb=back_transfered(HTS_STAT.stat_bytes,sback); \
  94.     /* Check */ \
  95.     { \
  96.       if (!RUN_CALLBACK7(opt, loop, sback->lnk, sback->count,-1,ptr,lien_tot,(int) (time_local()-HTS_STAT.stat_timestart),&HTS_STAT)) { \
  97.         return -1; \
  98.       } \
  99.     } \
  100.   } \
  101.   opt->state._hts_in_html_parsing = prev; \
  102. } while(0)
  103.  
  104.  
  105. // forme le nom du fichier α sauver (save) α partir de fil et adr
  106. // systΦme intelligent, qui renomme en cas de besoin (exemple: deux INDEX.HTML et index.html)
  107. int url_savename(char* adr_complete, char* fil_complete, char* save, 
  108.                                  char* former_adr, char* former_fil, 
  109.                                  char* referer_adr, char* referer_fil, 
  110.                                  httrackp* opt, 
  111.                                  lien_url** liens, int lien_tot, 
  112.                                  struct_back* sback, cache_back* cache, hash_struct* hash, 
  113.                                  int ptr, int numero_passe, const lien_back* headers) {
  114.     char catbuff[CATBUFF_SIZE];
  115.   const char* mime_type = ( headers && HTTP_IS_OK(headers->r.statuscode) ) ? headers->r.contenttype : NULL;
  116.   lien_back* const back = sback->lnk;
  117.   /* */
  118.   char BIGSTK newfil[HTS_URLMAXSIZE*2];   /* ="" */
  119.   /*char BIGSTK normadr_[HTS_URLMAXSIZE*2];*/
  120.   char BIGSTK normadr_[HTS_URLMAXSIZE*2], normfil_[HTS_URLMAXSIZE*2];
  121.     enum { PROTOCOL_HTTP, PROTOCOL_HTTPS, PROTOCOL_FTP, PROTOCOL_FILE, PROTOCOL_MMS, PROTOCOL_UNKNOWN };
  122.   static const char* protocol_str[] = {"http", "https", "ftp", "file", "mms", "unknown"};
  123.   int protocol = PROTOCOL_HTTP;
  124.   char* normadr;
  125.   char* normfil;
  126.   char* fil;
  127.   char* adr;
  128.   char* print_adr;
  129.   char *start_pos=NULL,*nom_pos=NULL,*dot_pos=NULL;  // Position nom et point
  130.   // pour changement d'extension ou de nom (content-disposition)
  131.   int ext_chg=0, ext_chg_delayed=0;
  132.   int is_html=0;
  133.   char ext[256];
  134.   int max_char=0;
  135.   //CLEAR
  136.   newfil[0]=ext[0]='\0';
  137.  
  138.   /* 8-3 ? */
  139.   switch(opt->savename_83) {
  140.   case 1:     // 8-3
  141.     max_char=8;
  142.     break;
  143.   case 2:     // Level 2 File names may be up to 31 characters.
  144.     max_char=31;
  145.     break;
  146.   default:
  147.     max_char=8;
  148.     break;
  149.   }
  150.  
  151.   // effacer save
  152.   save[0]='\0';
  153.   // fil
  154.   fil = fil_complete;
  155.   // copy of fil, used for lookups (see urlhack)
  156.   normfil = fil;
  157.   // et adr (sauter user/pass)
  158.   // on prend le parti de mettre les fichiers avec login/pass au mΩme endroit que si ils
  159.   // Θtaient capturΘs sans ces paramΦtres
  160.   // c'est pour cette raison qu'on ignore totalement adr_complete (mΩme pour la recherche en table de hachage)
  161.   adr = jump_identification(adr_complete);
  162.   // copy of adr, used for lookups (see urlhack)
  163.   normadr = adr;
  164.  
  165.   // normalize the URL:
  166.   // www.foo.com -> foo.com
  167.   // www-42.foo.com -> foo.com
  168.   // foo.com/bar//foobar -> foo.com/bar/foobar
  169.   if (opt->urlhack) {
  170.     // copy of adr (without protocol), used for lookups (see urlhack)
  171.     normadr=adr_normalized(adr, normadr_);
  172.     normfil=fil_normalized(fil,normfil_);
  173.   } else {
  174.     if (link_has_authority(adr_complete)) {     // https or other protocols : in "http/" subfolder
  175.       char* pos = strchr(adr_complete, ':');
  176.       if (pos != NULL) {
  177.         normadr_[0] = '\0';
  178.         strncatbuff(normadr_, adr_complete, (int)(pos - adr_complete));
  179.         strcatbuff(normadr_, "://");
  180.         strcatbuff(normadr_, normadr);
  181.         normadr=normadr_;
  182.       }
  183.     }
  184.   }
  185.  
  186.   // α afficher sans ftp://
  187.   print_adr=jump_protocol(adr);
  188.   if (strfield(adr_complete, "https:")) {
  189.       protocol = PROTOCOL_HTTPS;
  190.   } else if (strfield(adr_complete, "ftp:")) {
  191.       protocol = PROTOCOL_FTP;
  192.   } else if (strfield(adr_complete, "file:")) {
  193.       protocol = PROTOCOL_FILE;
  194.   } else if (strfield(adr_complete, "mms:")) {
  195.       protocol = PROTOCOL_MMS;
  196.   } else {
  197.       protocol = PROTOCOL_HTTP;
  198.   }
  199.  
  200.   // court-circuit pour lien primaire
  201.   if (strnotempty(adr)==0) {
  202.     if (strcmp(fil,"primary")==0) {
  203.       strcatbuff(save,"primary.html");
  204.       return 0;
  205.     }
  206.   }
  207.  
  208.  
  209.   // vΘrifier que le nom n'a pas dΘja ΘtΘ calculΘ (si oui le renvoyer tel que)
  210.   // vΘrifier que le nom n'est pas dΘja pris...
  211.   // NOTE: si on cherche /toto/ et que /toto est trouvΘ on le prend (et rΘciproquqment) ** // **
  212.   if (liens!=NULL) { 
  213.     int i;
  214.  
  215.     i=hash_read(hash,normadr,normfil,1,opt->urlhack);      // recherche table 1 (adr+fil)
  216.     if (i>=0) {    // ok, trouvΘ
  217.       strcpybuff(save,liens[i]->sav);
  218.       return 0;
  219.     }
  220.     i=hash_read(hash,normadr,normfil,2,opt->urlhack);      // recherche table 2 (former_adr+former_fil)
  221.     if (i>=0) {    // ok, trouvΘ
  222.       // copier location moved!
  223.       strcpybuff(adr_complete,liens[i]->adr);
  224.       strcpybuff(fil_complete,liens[i]->fil);
  225.       // et save
  226.       strcpybuff(save,liens[i]->sav);  // copier (formΘ α partir du nouveau lien!)
  227.       return 0;
  228.     }
  229.  
  230.     // chercher sans / ou avec / dans former
  231.     {
  232.       char BIGSTK fil_complete_patche[HTS_URLMAXSIZE*2];
  233.       strcpybuff(fil_complete_patche,normfil);
  234.       // Version avec ou sans /
  235.       if (fil_complete_patche[strlen(fil_complete_patche)-1]=='/')
  236.         fil_complete_patche[strlen(fil_complete_patche)-1]='\0';
  237.       else
  238.         strcatbuff(fil_complete_patche,"/");
  239.       i=hash_read(hash,normadr,fil_complete_patche,2,opt->urlhack);      // recherche table 2 (former_adr+former_fil)
  240.       if (i>=0) {
  241.         // Θcraser fil et adr (pas former_fil?????)
  242.         strcpybuff(adr_complete,liens[i]->adr);
  243.         strcpybuff(fil_complete,liens[i]->fil);
  244.         // Θcrire save
  245.         strcpybuff(save,liens[i]->sav);
  246.         return 0;
  247.       }
  248.     }
  249.   }
  250.  
  251.   // vΘrifier la non prΘsence de paramΦtres dans le nom de fichier
  252.   // si il y en a, les supprimer (ex: truc.cgi?subj=aspirateur)
  253.   // nΘanmoins, gardΘ pour vΘrifier la non duplication (voir aprΦs)
  254.   {
  255.     char* a;
  256.     a=strchr(fil,'?');
  257.     if (a!=NULL) {
  258.       strncatbuff(newfil,fil,(int) (a - fil));
  259.     } else {
  260.       strcpybuff(newfil,fil);
  261.     }
  262.     fil=newfil;
  263.   }
  264.   // Decode remaining %
  265.   strcpybuff(fil,unescape_http(catbuff,fil));
  266.     // , BUT do not decode high chars
  267.   //strcpybuff(fil,unescape_http_unharm(fil, 1));
  268.     // YES (not server side, but fs/client side)
  269.  
  270. #if HTS_USEMMS
  271.     /* .asx hack */
  272.     if (headers != NULL && headers->r.cdispo[0] != 0 
  273.         && strfield(headers->r.contenttype, "video/")
  274.         && strfield2(get_ext(OPT_GET_BUFF(opt),headers->r.cdispo), "asx") == 0) 
  275.     {
  276.         ext_chg = 1;
  277.         strcpybuff(ext, "asx");
  278.     }
  279.     else if (headers != NULL && headers->r.contenttype[0] != 0 
  280.         && strfield2(headers->r.contenttype, "video/x-ms-asf"))
  281.     {
  282.         char *exts = get_ext(OPT_GET_BUFF(opt),headers->url_fil);
  283.         if (strfield2(exts, "wmv") == 0)
  284.         {
  285.             ext_chg = 1;
  286.             strcpybuff(ext, "wmv");
  287.         }
  288.         else if (strfield2(exts, "asf") == 0)
  289.         {
  290.             ext_chg = 1;
  291.             strcpybuff(ext, "asf");
  292.         }
  293.         else if (strfield2(exts, "avi") == 0)
  294.         {
  295.             ext_chg = 1;
  296.             strcpybuff(ext, "avi");
  297.         }
  298.         else if (strfield2(exts, "asx") == 0)
  299.         {
  300.             ext_chg = 1;
  301.             strcpybuff(ext, "asx");
  302.         }
  303.     }
  304. #endif
  305.  
  306.   /* replace shtml to html.. */
  307.   if (opt->savename_delayed == 2)
  308.     is_html = -1;            /* ALWAYS delay type */
  309.   else
  310.     is_html = ishtml(opt,fil);
  311.   switch ( is_html ) {       /* .html,.shtml,.. */
  312.   case 1:
  313.     if ( 
  314.       (strfield2(get_ext(OPT_GET_BUFF(opt),fil),"html") == 0)
  315.       && (strfield2(get_ext(OPT_GET_BUFF(opt),fil),"htm") == 0)
  316.       )
  317.     {
  318.       strcpybuff(ext,"html");
  319.       ext_chg=1;
  320.     }
  321.     break;
  322.   case 0:
  323.     if (!strnotempty(ext)) {
  324.       if (is_userknowntype(opt,fil)) {      // mime known by user
  325.         char BIGSTK mime[1024];
  326.         mime[0]=ext[0]='\0';
  327.         get_userhttptype(opt,mime,fil);
  328.         if (strnotempty(mime)) {
  329.           give_mimext(ext,mime);
  330.           if (strnotempty(ext)) {
  331.             ext_chg=1;
  332.           }
  333.         }
  334.       }
  335.     }
  336.     break;
  337.   }
  338.  
  339.   // si option check_type activΘe
  340.   if (is_html < 0 && opt->check_type && !ext_chg) {
  341.     int ishtest=0;
  342.     if ( (!strfield(adr_complete,"file://")) 
  343.       && (!strfield(adr_complete,"ftp://")) 
  344. #if HTS_USEMMS
  345.       && (!strfield(adr_complete,"mms://")) 
  346. #endif
  347.       ) {
  348.       // tester type avec requΦte HEAD si on ne connait pas le type du fichier
  349.       if (!(   (opt->check_type==1) && (fil[strlen(fil)-1]=='/')   ))    // slash doit Ωtre html?
  350.       if ( opt->savename_delayed == 2 || (ishtest=ishtml(opt,fil)) < 0) { // on ne sait pas si c'est un html ou un fichier..
  351.         // lire dans le cache
  352.         htsblk r = cache_read(opt,cache,adr,fil,NULL,NULL);              // test uniquement
  353.         if (r.statuscode != -1) {  // pas d'erreur de lecture cache
  354.           char s[16]; s[0]='\0';
  355.           if ( (opt->debug>1) && (opt->log!=NULL) ) {
  356.             HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Testing link type (from cache) %s%s"LF,adr_complete,fil_complete);
  357.             test_flush;
  358.           }
  359.           if (strnotempty(r.cdispo)) {        /* filename given */
  360.             ext_chg=2;      /* change filename */
  361.             strcpybuff(ext,r.cdispo);
  362.           }
  363.           else if (!may_unknown(opt,r.contenttype) || ishtest == -2) {  // on peut patcher α priori?
  364.             give_mimext(s,r.contenttype);  // obtenir extension
  365.             if (strnotempty(s)>0) {        // on a reconnu l'extension
  366.               ext_chg=1;
  367.               strcpybuff(ext,s);
  368.             }
  369.           }
  370.           //
  371.         } else if ( opt->savename_delayed != 2 && is_userknowntype(opt,fil)) {   /* PATCH BY BRIAN SCHR╓DER. 
  372.                                                                             Lookup mimetype not only by extension, 
  373.                                                                             but also by filename */
  374.           /* Note: "foo.cgi => text/html" means that foo.cgi shall have the text/html MIME file type,
  375.           that is, ".html" */
  376.           char BIGSTK mime[1024];
  377.           mime[0]=ext[0]='\0';
  378.           get_userhttptype(opt, mime, fil);
  379.           if (strnotempty(mime)) {
  380.             give_mimext(ext, mime);
  381.             if (strnotempty(ext)) {
  382.               ext_chg=1;
  383.             }
  384.           }
  385.         }
  386.         // note: if savename_delayed is enabled, the naming will be temporary (and slightly invalid!)
  387.         else if (opt->savename_delayed != 0) {
  388.           if (mime_type != NULL)  {
  389.             ext[0] = '\0';
  390.             if (*mime_type) {
  391.               give_mimext(ext, mime_type);
  392.             }
  393.             if (strnotempty(ext)) {
  394.               char mime_from_file[128];
  395.               mime_from_file[0] = 0;
  396.               get_httptype(opt, mime_from_file, fil, 1);
  397.               if (!strnotempty(mime_from_file) || strcasecmp(mime_type, mime_from_file) != 0) {    /* different mime for this type */
  398.                 ext_chg = 1;
  399.               } else {
  400.                 ext_chg = 0;
  401.               }
  402.             }
  403.           } else {
  404.             /* Avoid collisions (no collisionning detection) */
  405.             sprintf(ext, "%x.%s", opt->state.delayedId++, DELAYED_EXT);
  406.             ext_chg = 1;
  407.                         ext_chg_delayed = 1;        /* due to naming system */
  408.           }
  409.         }
  410.         // test imposible dans le cache, faire une requΩte
  411.         else {
  412.           //
  413.           int hihp = opt->state._hts_in_html_parsing;
  414.           int has_been_moved=0;
  415.           char BIGSTK curr_adr[HTS_URLMAXSIZE*2],curr_fil[HTS_URLMAXSIZE*2];
  416.           
  417.           /* Ensure we don't use too many sockets by using a "testing" one
  418.              If we have only 1 simultaneous connection authorized, wait for pending download
  419.              Wait for an available slot 
  420.           */
  421.           URLSAVENAME_WAIT_FOR_AVAILABLE_SOCKET();
  422.  
  423.           /* Rock'in */
  424.           curr_adr[0]=curr_fil[0]='\0';
  425.          opt->state. _hts_in_html_parsing=2;  // test
  426.           if ( (opt->debug>1) && (opt->log!=NULL) ) {
  427.             HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Testing link type %s%s"LF,adr_complete,fil_complete);
  428.             test_flush;
  429.           }
  430.           strcpybuff(curr_adr,adr_complete);
  431.           strcpybuff(curr_fil,fil_complete);
  432.           // ajouter dans le backing le fichier en mode test
  433.           // savename: rien car en mode test
  434.           if (back_add(sback,opt,cache,curr_adr,curr_fil,BACK_ADD_TEST,referer_adr,referer_fil,1)!=-1) {
  435.             int b;
  436.             b=back_index(opt,sback,curr_adr,curr_fil,BACK_ADD_TEST);         
  437.             if (b>=0) {
  438.               int stop_looping=0;
  439.               int petits_tours=0;
  440.               int get_test_request=0;       // en cas de bouclage sur soi mΩme avec HEAD, tester avec GET.. parfois c'est la cause des problΦmes
  441.               do {
  442.                 // temps α attendre, et remplir autant que l'on peut le cache (backing)
  443.                 if (back[b].status>0) {
  444.                   back_wait(sback,opt,cache,0);        
  445.                 }
  446.                 if (ptr>=0) {
  447.                                     back_fillmax(sback,opt,cache,liens,ptr,numero_passe,lien_tot);
  448.                                 }
  449.  
  450.                                 // on est obligΘ d'appeler le shell pour le refresh..
  451.                                 // Transfer rate
  452.                                 engine_stats();
  453.  
  454.                                 // Refresh various stats
  455.                                 HTS_STAT.stat_nsocket=back_nsoc(sback);
  456.                                 HTS_STAT.stat_errors=fspc(opt,NULL,"error");
  457.                                 HTS_STAT.stat_warnings=fspc(opt,NULL,"warning");
  458.                                 HTS_STAT.stat_infos=fspc(opt,NULL,"info");
  459.                                 HTS_STAT.nbk=backlinks_done(sback,liens,lien_tot,ptr);
  460.                                 HTS_STAT.nb=back_transfered(HTS_STAT.stat_bytes,sback);
  461.  
  462.                 if (!RUN_CALLBACK7(opt, loop, sback->lnk, sback->count,b,ptr,lien_tot,(int) (time_local()-HTS_STAT.stat_timestart),&HTS_STAT)) {
  463.                   return -1;
  464.                 } else if (opt->state._hts_cancel || !back_checkmirror(opt)) {    // cancel 2 ou 1 (cancel parsing)
  465.                   back_delete(opt,cache,sback,b);       // cancel test
  466.                   stop_looping = 1;
  467.                 }
  468.  
  469.                 // traitement des 304,303..
  470.                 if (back[b].status<=0) {
  471.                   if (HTTP_IS_REDIRECT(back[b].r.statuscode)) {    // agh moved.. un tit tour de plus
  472.                     if ((petits_tours<5) && (former_adr) && (former_fil)) { // on va pas tourner en rond non plus!
  473.                       if ((int) strnotempty(back[b].r.location)) {    // location existe!
  474.                         char BIGSTK mov_url[HTS_URLMAXSIZE*2],mov_adr[HTS_URLMAXSIZE*2],mov_fil[HTS_URLMAXSIZE*2];
  475.                         mov_url[0]=mov_adr[0]=mov_fil[0]='\0';
  476.                         //
  477.                         strcpybuff(mov_url,back[b].r.location);    // copier URL
  478.                         if (ident_url_relatif(mov_url,curr_adr,curr_fil,mov_adr,mov_fil)>=0) {                        
  479.                           // si non bouclage sur soi mΩme, ou si test avec GET non testΘ
  480.                           if ((strcmp(mov_adr,curr_adr)) || (strcmp(mov_fil,curr_fil)) || (get_test_request==0)) {
  481.                             // bouclage?
  482.                             if ((!strcmp(mov_adr,curr_adr)) && (!strcmp(mov_fil,curr_fil)))
  483.                               get_test_request=1;     // faire requΦte avec GET
  484.  
  485.                             // recopier former_adr/fil?
  486.                             if ((former_adr) && (former_fil)) {
  487.                               if (strnotempty(former_adr)==0) {    // Pas dΘja notΘ
  488.                                 strcpybuff(former_adr,curr_adr);
  489.                                 strcpybuff(former_fil,curr_fil);
  490.                               }
  491.                             }
  492.  
  493.                             // check explicit forbidden - don't follow 3xx in this case
  494.                             {
  495.                               int set_prio_to=0;
  496.                               if (hts_acceptlink(opt,ptr,lien_tot,liens,
  497.                                 mov_adr,mov_fil,
  498.                                 NULL, NULL,
  499.                                 &set_prio_to,
  500.                                 NULL) == 1) 
  501.                               {  /* forbidden */
  502.                                 has_been_moved = 1;
  503.                                 back_maydelete(opt,cache,sback,b);    // ok
  504.                                 strcpybuff(curr_adr,mov_adr);
  505.                                 strcpybuff(curr_fil,mov_fil);
  506.                                 mov_url[0]='\0';
  507.                                 stop_looping = 1;
  508.                               }
  509.                             }
  510.                             
  511.                             // ftp: stop!
  512.                             if (strfield(mov_url,"ftp://")
  513. #if HTS_USEMMS
  514.                                                             || strfield(mov_url,"mms://")
  515. #endif
  516.                                                             ) 
  517.                                                         {    // ftp, ok on arrΩte
  518.                               has_been_moved = 1;
  519.                               back_maydelete(opt,cache,sback,b);    // ok
  520.                               strcpybuff(curr_adr,mov_adr);
  521.                               strcpybuff(curr_fil,mov_fil);
  522.                               stop_looping = 1;
  523.                             } else if (*mov_url) {
  524.                               char* methode;
  525.                               if (!get_test_request)
  526.                                 methode=BACK_ADD_TEST;      // tester avec HEAD
  527.                               else {
  528.                                 methode=BACK_ADD_TEST2;     // tester avec GET
  529.                                 if ( opt->log!=NULL ) {
  530.                                   HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Loop with HEAD request (during prefetch) at %s%s"LF,curr_adr,curr_fil);
  531.                                   test_flush;
  532.                                 }                    
  533.                               }
  534.                               // Ajouter
  535.                               URLSAVENAME_WAIT_FOR_AVAILABLE_SOCKET();
  536.                               if (back_add(sback,opt,cache,mov_adr,mov_fil,methode,referer_adr,referer_fil,1)!=-1) {    // OK
  537.                                 if ( (opt->debug>1) && (opt->log!=NULL) ) {
  538.                                   HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"(during prefetch) %s (%d) to link %s at %s%s"LF,back[b].r.msg,back[b].r.statuscode,back[b].r.location,curr_adr,curr_fil);
  539.                                   test_flush;
  540.                                 }
  541.                                 
  542.                                 // libΘrer emplacement backing actuel et attendre le prochain
  543.                                 back_maydelete(opt,cache,sback,b);
  544.                                 strcpybuff(curr_adr,mov_adr);
  545.                                 strcpybuff(curr_fil,mov_fil);
  546.                                 b=back_index(opt,sback,curr_adr,curr_fil,methode);         
  547.                                 if (!get_test_request)
  548.                                   has_been_moved = 1;       // sinon ne pas forcer has_been_moved car non dΘplacΘ
  549.                                 petits_tours++;
  550.                                 //
  551.                               } else {// sinon on fait rien et on s'en va.. (ftp etc)
  552.                                 if ( (opt->debug>1)  && (opt->log)) {
  553.                                   HTS_LOG(opt,LOG_DEBUG); fprintf(opt->log,"Warning: Savename redirect backing error at %s%s"LF,mov_adr,mov_fil);
  554.                                   test_flush;
  555.                                 } 
  556.                               }
  557.                             }
  558.                           } else {
  559.                             if ( opt->log!=NULL ) {
  560.                               HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Unable to test %s%s (loop to same filename)"LF,adr_complete,fil_complete);
  561.                               test_flush;
  562.                             }
  563.                           }
  564.                           
  565.                         }
  566.                       }
  567.                     } else{  // arrΩter les frais
  568.                       if ( opt->log!=NULL ) {
  569.                         HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Unable to test %s%s (loop)"LF,adr_complete,fil_complete);
  570.                         test_flush;
  571.                       }
  572.                     }
  573.                   }  // ok, leaving
  574.                 }
  575.               } while(!stop_looping && back[b].status > 0 && back[b].status < 1000);
  576.               
  577.               // Si non dΘplacΘ, forcer type?
  578.               if (!has_been_moved) {
  579.                 if (back[b].r.statuscode!=-10) {    // erreur
  580.                   if (strnotempty(back[b].r.contenttype)==0)
  581.                     strcpybuff(back[b].r.contenttype,"text/html");    // message d'erreur en html
  582.                   // Finalement on, renvoie un erreur, pour ne toucher α rien dans le code
  583.                   // libΘrer emplacement backing
  584.                   /*if (opt->log!=NULL) {
  585.                     fspc(opt->log,0); fprintf(opt->log,"Error: (during prefetch) %s (%d) to link %s at %s%s"LF,back[b].r.msg,back[b].r.statuscode,back[b].r.location,curr_adr,curr_fil);
  586.                     test_flush;
  587.                   }                    
  588.                   back_delete(opt,cache,sback,b);
  589.                   return -1;        // ERREUR (404 par exemple)
  590.                   */
  591.                 } 
  592.                 
  593.                 {            // pas d'erreur, changer type?
  594.                   char s[16];
  595.                   s[0]='\0';
  596.                   if (strnotempty(back[b].r.cdispo)) {        /* filename given */
  597.                     ext_chg=2;      /* change filename */
  598.                     strcpybuff(ext,back[b].r.cdispo);
  599.                   }
  600.                   else if (!may_unknown(opt,back[b].r.contenttype) || ishtest == -2 ) {  // on peut patcher α priori? (pas interdit ou pas de type)
  601.                     give_mimext(s,back[b].r.contenttype);  // obtenir extension
  602.                     if (strnotempty(s)>0) {    // on a reconnu l'extension
  603.                       ext_chg=1;
  604.                       strcpybuff(ext,s);
  605.                     }
  606.                   }
  607.                 }
  608.               }
  609.               // FIN Si non dΘplacΘ, forcer type?
  610.  
  611.               // libΘrer emplacement backing
  612.               back_maydelete(opt,cache,sback,b);
  613.               
  614.               // --- --- ---
  615.               // oops, a ΘtΘ dΘplacΘ.. on recalcule en rΘcursif (osons!)
  616.               if (has_been_moved) {
  617.                 // copier adr, fil (optionnel, mais sinon marche pas pour le rip)
  618.                 strcpybuff(adr_complete,curr_adr);
  619.                 strcpybuff(fil_complete,curr_fil);
  620.                 // copier adr, fil
  621.                 
  622.                 return url_savename(curr_adr,curr_fil,save,NULL,NULL,referer_adr,referer_fil,opt,liens,lien_tot,sback,cache,hash,ptr,numero_passe,NULL);
  623.               }
  624.               // --- --- ---
  625.               
  626.             }
  627.             
  628.           } else {
  629.             printf("PANIC! : Savename Crash adding error, unexpected error found.. [%d]\n",__LINE__);
  630. #if BDEBUG==1
  631.             printf("error while savename crash adding\n");
  632. #endif
  633.             if (opt->log) {
  634.               HTS_LOG(opt,LOG_ERROR); fprintf(opt->log,"Unexpected savename backing error at %s%s"LF,adr,fil_complete);
  635.               test_flush;
  636.             } 
  637.             
  638.           }
  639.           // restaurer
  640.           opt->state._hts_in_html_parsing=hihp;
  641.         }  // cachΘ?
  642.       }
  643.     }
  644.   }
  645.  
  646.  
  647.  
  648.   // - - - DEBUT NOMMAGE - - -
  649.  
  650.   // Donner nom par dΘfaut?
  651.   if (fil[strlen(fil)-1]=='/')  {
  652.     if (!strfield(adr_complete,"ftp://")
  653. #if HTS_USEMMS
  654.             && !strfield(adr_complete,"mms://")
  655. #endif
  656.             )
  657.         {
  658.       strcatbuff(fil,DEFAULT_HTML);     // nommer page par dΘfaut!!
  659. #if HTS_USEMMS
  660.         } else if (strfield(adr_complete,"mms://")) {
  661.             strcatbuff(fil,DEFAULT_MMS);
  662. #endif
  663.         } else {
  664.       if (!opt->proxy.active)
  665.         strcatbuff(fil,DEFAULT_FTP);     // nommer page par dΘfaut (texte)
  666.       else
  667.         strcatbuff(fil,DEFAULT_HTML);     // nommer page par dΘfaut (α priori ici html depuis un proxy http)
  668.     }
  669.   }
  670.   // Changer extension?
  671.   // par exemple, php3 sera sauvΘ en html, cgi en html ou gif, xbm etc.. selon les cas
  672.   if (ext_chg) {  // changer ext
  673.     char* a=fil+strlen(fil)-1;
  674.     if ( (opt->debug>1) && (opt->log!=NULL) ) {
  675.       HTS_LOG(opt,LOG_DEBUG);
  676.       if (ext_chg==1)
  677.         fprintf(opt->log,"Changing link extension %s%s to .%s"LF,adr_complete,fil_complete,ext);
  678.       else
  679.         fprintf(opt->log,"Changing link name %s%s to %s"LF,adr_complete,fil_complete,ext);
  680.       test_flush;
  681.     }
  682.     if (ext_chg==1) {
  683.       while((a > fil) && (*a!='.') && (*a!='/')) a--;
  684.       if (*a=='.') *a='\0';  // couper
  685.       strcatbuff(fil,".");      // recopier point
  686.     } else {
  687.       while(( a > fil) && (*a!='/')) a--;
  688.       if (*a=='/') a++;
  689.       *a='\0';
  690.    }
  691.     strcatbuff(fil,ext);    // copier ext/nom
  692.   }
  693.  
  694.   // Rechercher premier / et dernier .
  695.   {  
  696.     char* a=fil+strlen(fil)-1;
  697.  
  698.     // passer structures
  699.     start_pos=fil;
  700.     while(( a > fil) && (*a != '/') && (*a != '\\')) {
  701.       if (*a == '.')    // point? noter position
  702.         if (!dot_pos)
  703.           dot_pos=a;
  704.         a--;
  705.     }
  706.     if ((*a=='/') || (*a=='\\')) a++;
  707.     nom_pos = a;
  708.   }
  709.  
  710.   
  711.   // un nom de fichier est gΘnΘrΘ
  712.   // s'il existe dΘja, alors on le mofifie lΘgΦrement
  713.  
  714.   // ajouter nom du site Θventuellement en premier
  715.   if (opt->savename_type == -1) {    // utiliser savename_userdef! (%h%p/%n%q.%t)
  716.     const char* a = StringBuff(opt->savename_userdef);
  717.     char* b = save;
  718.     /*char *nom_pos=NULL,*dot_pos=NULL;  // Position nom et point */
  719.     char tok;
  720.  
  721.     /*
  722.     {  // Rechercher premier /
  723.       char* a=fil+strlen(fil)-1;
  724.       // passer structures
  725.       while(((int) a>(int) fil) && (*a != '/') && (*a != '\\')) {
  726.         if (*a == '.')    // point? noter position
  727.         if (!dot_pos)
  728.           dot_pos=a;
  729.         a--;
  730.       }
  731.       if ((*a=='/') || (*a=='\\')) a++;
  732.       nom_pos = a;
  733.     }
  734.     */
  735.  
  736.     // Construire nom
  737.     while ((*a) && (((int) (b - save)) < HTS_URLMAXSIZE ) ) {    // parser, et pas trop long..
  738.       if (*a == '%') {
  739.         int short_ver=0;
  740.         a++;
  741.         if (*a == 's') {
  742.           short_ver=1;
  743.           a++;
  744.         }
  745.         *b='\0';
  746.         switch(tok=*a++) {
  747.           case '[':            // %[param:prefix_if_not_empty:suffix_if_not_empty:empty_replacement:notfound_replacement]
  748.             if (strchr(a,']')) {
  749.               int pos=0;
  750.               char name[5][256];
  751.               char* c=name[0];
  752.               for(pos = 0 ; pos < 5 ; pos++) {
  753.                 name[pos][0]='\0';
  754.               }
  755.               pos=0;
  756.               while(*a!=']') {
  757.                 if (pos < 5) {
  758.                   if (*a == ':') {  // next token
  759.                     c=name[++pos];
  760.                     a++;
  761.                   } else {
  762.                     *c++=*a++;
  763.                     *c='\0';
  764.                   }
  765.                 }
  766.               }
  767.               a++;
  768.               strcatbuff(name[0],"=");           /* param=.. */
  769.               c=strchr(fil_complete,'?');
  770.               /* parameters exists */
  771.               if (c) {
  772.                 char* cp;
  773.                 while((cp = strstr(c+1, name[0])) && *(cp-1) != '?' && *(cp-1) != '&') {     /* finds [?&]param= */
  774.                   c = cp;
  775.                 }
  776.                 if (cp) {
  777.                   c = cp + strlen(name[0]);    /* jumps "param=" */
  778.                   strcpybuff(b, name[1]);    /* prefix */
  779.                   b += strlen(b);
  780.                   if (*c != '\0' && *c != '&') {
  781.                     char* d = name[0];
  782.                     /* */
  783.                     while(*c != '\0' && *c != '&') {
  784.                       *d++ = *c++;
  785.                     }
  786.                     *d = '\0';
  787.                     d = unescape_http(catbuff,name[0]);
  788.                     if (d && *d) {
  789.                       strcpybuff(b, d);         /* value */
  790.                       b += strlen(b);
  791.                     } else {
  792.                       strcpybuff(b, name[3]);    /* empty replacement if any */
  793.                       b += strlen(b);
  794.                     }
  795.                   } else {
  796.                     strcpybuff(b, name[3]);    /* empty replacement if any */
  797.                     b += strlen(b);
  798.                   }
  799.                   strcpybuff(b, name[2]);    /* suffix */
  800.                   b += strlen(b);
  801.                 } else {
  802.                   strcpybuff(b, name[4]);    /* not found replacement if any */
  803.                   b += strlen(b);
  804.                 }
  805.                             } else {
  806.                                 strcpybuff(b, name[4]);    /* not found replacement if any */
  807.                                 b += strlen(b);
  808.                             }
  809.             }
  810.           break;
  811.           case '%': *b++='%'; break;
  812.           case 'n':    // nom sans ext
  813.             *b='\0';
  814.             if (dot_pos) {
  815.               if (!short_ver)    // Noms longs
  816.                 strncatbuff(b,nom_pos,(int) (dot_pos - nom_pos));
  817.               else
  818.                 strncatbuff(b,nom_pos,min((int) (dot_pos - nom_pos),8));
  819.             } else {
  820.               if (!short_ver)    // Noms longs
  821.                 strcpybuff(b,nom_pos);
  822.               else
  823.                 strncatbuff(b,nom_pos,8);
  824.             }
  825.             b+=strlen(b);   // pointer α la fin
  826.             break;
  827.           case 'N':    // nom avec ext
  828.             // RECOPIE NOM + EXT
  829.             *b='\0';
  830.             if (dot_pos) {
  831.               if (!short_ver)    // Noms longs
  832.                 strncatbuff(b,nom_pos,(int) (dot_pos - nom_pos));
  833.               else
  834.                 strncatbuff(b,nom_pos,min((int) (dot_pos - nom_pos),8));
  835.             } else {
  836.               if (!short_ver)    // Noms longs
  837.                 strcpybuff(b,nom_pos);
  838.               else
  839.                 strncatbuff(b,nom_pos,8);
  840.             }
  841.             b+=strlen(b);   // pointer α la fin
  842.             // RECOPIE NOM + EXT
  843.             *b='\0';
  844.             if (dot_pos) {
  845.               if (!short_ver)    // Noms longs
  846.                 strcpybuff(b,dot_pos+1);
  847.               else
  848.                 strncatbuff(b,dot_pos+1,3);
  849.             } else {
  850.               if (!short_ver)    // Noms longs
  851.                 strcpybuff(b,DEFAULT_EXT + 1);    // pas de..
  852.               else
  853.                 strcpybuff(b,DEFAULT_EXT_SHORT + 1);    // pas de..
  854.             }
  855.             b+=strlen(b);   // pointer α la fin
  856.             //
  857.             break;
  858.           case 't':    // ext
  859.             *b='\0';
  860.             if (dot_pos) {
  861.               if (!short_ver)    // Noms longs
  862.                 strcpybuff(b,dot_pos+1);
  863.               else
  864.                 strncatbuff(b,dot_pos+1,3);
  865.             } else {
  866.               if (!short_ver)    // Noms longs
  867.                 strcpybuff(b,DEFAULT_EXT + 1);    // pas de..
  868.               else
  869.                 strcpybuff(b,DEFAULT_EXT_SHORT + 1);    // pas de..
  870.             }
  871.             b+=strlen(b);   // pointer α la fin
  872.             break;
  873.           case 'p':    // path sans dernier /
  874.             *b='\0';
  875.             if (nom_pos != fil + 1) { // pas: /index.html (chemin nul)
  876.               if (!short_ver) {   // Noms longs
  877.                 strncatbuff(b,fil,(int) (nom_pos - fil) - 1);
  878.               } else {
  879.                 char BIGSTK pth[HTS_URLMAXSIZE*2],n83[HTS_URLMAXSIZE*2];
  880.                 pth[0]=n83[0]='\0';
  881.                 //
  882.                 strncatbuff(pth,fil,(int) (nom_pos - fil) - 1);
  883.                 long_to_83(opt->savename_83,n83,pth);
  884.                 strcpybuff(b,n83);
  885.               }
  886.             }
  887.             b+=strlen(b);   // pointer α la fin
  888.             break;
  889.           case 'h':    // host
  890.             *b='\0';
  891.             if (strcmp(adr_complete,"file://")==0) {
  892.               if (!short_ver)    // Noms longs
  893.                 strcpybuff(b,"localhost");
  894.               else
  895.                 strcpybuff(b,"local");
  896.             } else {
  897.               if (!short_ver)    // Noms longs
  898.                 strcpybuff(b,print_adr);
  899.               else
  900.                 strncatbuff(b,print_adr,8);
  901.             }
  902.             b+=strlen(b);   // pointer α la fin
  903.             break;
  904.           case 'M':         /* host/address?query MD5 (128-bits) */
  905.             *b='\0';
  906.             {
  907.               char digest[32+2];
  908.               char BIGSTK buff[HTS_URLMAXSIZE*2];
  909.               digest[0]=buff[0]='\0';
  910.               strcpybuff(buff,adr);
  911.               strcatbuff(buff,fil_complete);
  912.               domd5mem(buff,strlen(buff),digest,1);
  913.               strcpybuff(b,digest);
  914.             }
  915.             b+=strlen(b);   // pointer α la fin
  916.             break;
  917.           case 'Q': case 'q':         /* query MD5 (128-bits/16-bits) 
  918.                                          GENERATED ONLY IF query string exists! */
  919.                         {
  920.                             char md5[32 + 2];
  921.                             *b='\0';
  922.                             strncatbuff(b,url_md5(md5, fil_complete),(tok == 'Q')?32:4);
  923.                             b+=strlen(b);   // pointer α la fin
  924.                         }
  925.             break;
  926.           case 'r': case 'R':        // protocol
  927.             *b='\0';
  928.             strcatbuff(b, protocol_str[protocol]);
  929.             b+=strlen(b);   // pointer α la fin
  930.           break;
  931.  
  932.       /* Patch by Juan Fco Rodriguez to get the full query string */
  933.       case 'k':
  934.         {
  935.           char *d = strchr(fil_complete,'?');
  936.           if( d != NULL )
  937.           {
  938.             strcatbuff(b, d);
  939.             b+=strlen(b);
  940.           }
  941.         }
  942.         break;
  943.  
  944.         }
  945.       } else
  946.         *b++=*a++;
  947.     }
  948.     *b++='\0';
  949.     //
  950.     // Types prΘdΘfinis
  951.     //
  952.  
  953.   } 
  954.   //
  955.   // Structure originale
  956.   else if (opt->savename_type%100==0) { 
  957.     /* recopier www.. */
  958.     if (opt->savename_type!=100) {  
  959.       if (((opt->savename_type/1000)%2)==0) {    // >1000 signifie "pas de www/"
  960.         if (strcmp(adr_complete,"file://")==0) {
  961.           //## if (*adr==lOCAL_CHAR) {
  962.           if (opt->savename_83 != 1)  // noms longs
  963.             strcatbuff(save,"localhost");
  964.           else
  965.             strcatbuff(save,"local");
  966.         } else {
  967.           // adresse url
  968.           if (!opt->savename_83) {  // noms longs (et pas de .)
  969.             strcatbuff(save,print_adr);
  970.           } else {  // noms 8-3
  971.             if (strlen(print_adr)>4) {
  972.               if (strfield(print_adr,"www."))
  973.                 strncatbuff(save,print_adr+4,max_char);
  974.               else
  975.                 strncatbuff(save,print_adr,8);
  976.             } else strncatbuff(save,print_adr,max_char);
  977.           }
  978.         }
  979.         if (*fil!='/') strcatbuff(save,"/");
  980.       }
  981.     }
  982.   
  983.     hts_lowcase(save);
  984.         
  985.     /*
  986.     // ne sert α rien car a dΘja ΘtΘ filtrΘ normalement
  987.     if ((*fil=='.') && (*(fil+1)=='/'))          // ./index.html ** //
  988.       url_savename_addstr(save,fil+2);
  989.     else                                               // index.html ou /index.html
  990.       url_savename_addstr(save,fil);
  991.     if (save[strlen(save)-1]=='/') 
  992.       strcatbuff(save,DEFAULT_HTML);     // nommer page par dΘfaut!!
  993. */
  994.  
  995.     /* add name */
  996.     ADD_STANDARD_PATH;
  997.     ADD_STANDARD_NAME(0);
  998.  
  999.   }
  1000.   //
  1001.   // Structure html/image
  1002.   else {    
  1003.     // dossier "web" ou "www.xxx" ?
  1004.     if (((opt->savename_type/1000)%2)==0) {    // >1000 signifie "pas de www/"
  1005.       if ((opt->savename_type/100)%2) {
  1006.         if (strcmp(adr_complete,"file://")==0) {
  1007.         //## if (*adr==lOCAL_CHAR) {
  1008.           if (opt->savename_83 != 1)  // noms longs
  1009.             strcatbuff(save,"localhost/");
  1010.           else
  1011.             strcatbuff(save,"local/");
  1012.         } else {
  1013.           // adresse url
  1014.           if (!opt->savename_83) {  // noms longs
  1015.             strcatbuff(save,print_adr); strcatbuff(save,"/");
  1016.           } else {  // noms 8-3
  1017.             if (strlen(print_adr)>4) {
  1018.               if (strfield(print_adr,"www."))
  1019.                 strncatbuff(save,print_adr+4,max_char);
  1020.               else
  1021.                 strncatbuff(save,print_adr,max_char);
  1022.               strcatbuff(save,"/");
  1023.             } else { 
  1024.               strncatbuff(save,print_adr,max_char); strcatbuff(save,"/");
  1025.             }
  1026.           }
  1027.         }
  1028.       } else {
  1029.         strcatbuff(save,"web/");    // rΘpertoire gΘnΘral
  1030.       }
  1031.     } 
  1032.  
  1033.     // si un html α coup s√r
  1034.     if ( (ext_chg!=0) ? (ishtml_ext(ext) == 1) : (ishtml(opt,fil)==1) ) {
  1035.       if (opt->savename_type%100==2) {  // html/
  1036.         strcatbuff(save, "html/");
  1037.       }
  1038.     } else {
  1039.       if ((opt->savename_type%100==1) || (opt->savename_type%100==2)) {  // html & images
  1040.         strcatbuff(save, "images/");
  1041.       }
  1042.     }
  1043.     
  1044.     switch (opt->savename_type%100) {
  1045.     case 4: case 5: {           // sΘparer par types
  1046.       char* a=fil+strlen(fil)-1;
  1047.       // passer structures
  1048.       while(( a > fil) && (*a != '/') && (*a != '\\')) a--;      
  1049.       if ((*a=='/') || (*a=='\\')) a++;
  1050.  
  1051.       // html?
  1052.       if ( (ext_chg!=0) ? (ishtml_ext(ext)==1) : (ishtml(opt,fil)==1) ) {
  1053.         if (opt->savename_type%100==5)
  1054.           strcatbuff(save,"html/");
  1055.       } else {
  1056.         char* a=fil+strlen(fil)-1;
  1057.         while(( a> fil) && (*a != '/') && (*a != '.')) a--;      
  1058.         if (*a!='.')
  1059.           strcatbuff(save,"other");
  1060.         else
  1061.           strcatbuff(save,a+1);
  1062.         strcatbuff(save,"/");
  1063.       }
  1064.       /*strcatbuff(save,a);*/
  1065.       /* add name */
  1066.       ADD_STANDARD_NAME(0);
  1067.             }
  1068.       break;
  1069.     case 99: {                  // 'codΘ' .. c'est un gadget
  1070.       int i;
  1071.       int j;
  1072.       char* a;
  1073.       char C[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
  1074.       int L;
  1075.       // pseudo-CRC sur fil et adr pour initialiser gΘnΘrateur alΘatoire..
  1076.       unsigned int s=0;
  1077.       L = (int) strlen(C);
  1078.       for(i=0;i<(int) strlen(fil_complete);i++) {
  1079.         s+=(unsigned int) fil_complete[i];
  1080.       }
  1081.       for(i=0;i<(int) strlen(adr_complete);i++) {
  1082.         s+=(unsigned int) adr_complete[i];
  1083.       }
  1084.       srand(s);
  1085.       
  1086.       j = (int) strlen(save);
  1087.       for(i=0;i<8;i++) {
  1088.         char c=C[(rand()%L)];
  1089.         save[i+j]=c;
  1090.       }
  1091.       save[i+j]='\0';
  1092.       // ajouter extension
  1093.       a = fil + strlen(fil) - 1;
  1094.       while(( a > fil) && (*a != '/') && (*a != '.')) a--;
  1095.       if (*a=='.') {
  1096.         strcatbuff(save,a);    // ajouter
  1097.       }
  1098.              } 
  1099.       break;
  1100.     default: {   // noms sans les noms des rΘpertoires
  1101.       // ne garder que le nom, pas la structure
  1102.       /*
  1103.       char* a=fil+strlen(fil)-1;
  1104.       while(((int) a>(int) fil) && (*a != '/') && (*a != '\\')) a--;      
  1105.       if ((*a=='/') || (*a=='\\')) a++;
  1106.       strcatbuff(save,a);
  1107.       */
  1108.  
  1109.       /* add name */
  1110.       ADD_STANDARD_NAME(0);
  1111.             }
  1112.       break;
  1113.     }
  1114.  
  1115.     hts_lowcase(save);
  1116.  
  1117.     if (save[strlen(save)-1]=='/') 
  1118.       strcatbuff(save,DEFAULT_HTML);     // nommer page par dΘfaut!!
  1119.   }
  1120.  
  1121.  
  1122.   // vΘrifier qu'on ne doit pas forcer l'extension
  1123.   // par exemple, asp sera sauvΘ en html, cgi en html ou gif, xbm etc.. selon les cas
  1124.   /*if (ext_chg) {
  1125.     char* a=save+strlen(save)-1;
  1126.     while(((int) a>(int) save) && (*a!='.') && (*a!='/')) a--;
  1127.     if (*a=='.') *a='\0';  // couper
  1128.     // recopier extension
  1129.     strcatbuff(save,".");
  1130.     strcatbuff(save,ext);    // copier ext
  1131.   }*/
  1132.   // de mΩme en cas de manque d'extension on en place une de maniΦre forcΘe..
  1133.   // cela Θvite les /chez/toto et les /chez/toto/index.html incompatibles
  1134.   if (opt->savename_type != -1) {
  1135.     char* a=save+strlen(save)-1;
  1136.     while(( a > save) && (*a!='.') && (*a!='/')) a--;
  1137.     if (*a!='.') {   // agh pas de point
  1138.       //strcatbuff(save,".none");                 // a Θviter
  1139.       strcatbuff(save,".html");                   // prΘfΘrable!
  1140.       if ( (opt->debug>1) && (opt->log!=NULL) ) {
  1141.         HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"Default HTML type set for %s%s"LF,adr_complete,fil_complete);
  1142.         test_flush;
  1143.       }
  1144.     }
  1145.   }
  1146.  
  1147.   // effacer pass au besoin pour les autentifications
  1148.   // (plus la peine : masquΘ au dΘbut)
  1149. /*
  1150.   {
  1151.     char* a=jump_identification(save);
  1152.     if (a!=save) {
  1153.       char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1154.       char *b;
  1155.       tempo[0]='\0';
  1156.       strcpybuff(tempo,"[");
  1157.       b=strchr(save,':');
  1158.       if (!b) b=strchr(save,'@');
  1159.       if (b)
  1160.         strncatbuff(tempo,save,(int) b-(int) a);
  1161.       strcatbuff(tempo,"]");
  1162.       strcatbuff(tempo,a);
  1163.       strcpybuff(save,a);
  1164.     }
  1165.   }
  1166. */
  1167.  
  1168.   // Θviter les / au dΘbut (cause: N100)
  1169.   if (save[0]=='/') {
  1170.     char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1171.     strcpybuff(tempo,save+1);
  1172.     strcpybuff(save,tempo);
  1173.   }
  1174.  
  1175.   // changer les ~,:,",*,? en _ pour sauver sur disque
  1176.   hts_replace(save,'~','_');  // interdit sous unix (~foo)
  1177.   //
  1178.   hts_replace(save,'\\','_');
  1179.   hts_replace(save,':','_');  // interdit sous windows
  1180.   hts_replace(save,'*','_');  // interdit sous windows
  1181.   hts_replace(save,'?','_');  // doit pas arriver!!
  1182.   hts_replace(save,'\"','_');  // interdit sous windows
  1183.   hts_replace(save,'<','_');  // interdit sous windows
  1184.   hts_replace(save,'>','_');  // interdit sous windows
  1185.   hts_replace(save,'|','_');  // interdit sous windows
  1186.   //
  1187.   hts_replace(save,'@','_');
  1188.   if (opt->savename_83 == 2) { // CDROM
  1189.     // maybe other ones?
  1190.     hts_replace(save,'-','_');
  1191.     hts_replace(save,'=','_');
  1192.     hts_replace(save,'+','_');
  1193.   }
  1194.   //
  1195.   { // Θliminer les // (comme ftp://)
  1196.     char* a;
  1197.     while( (a=strstr(save,"//")) ) *a='_';
  1198.     // Eliminer chars spΘciaux
  1199.     a=save -1 ;
  1200.     while(*(++a))
  1201.       if ( ((unsigned char)(*a) <= 31)
  1202.           || ((unsigned char)(*a) == 127) )
  1203.         *a='_';
  1204.   }
  1205.  
  1206.    
  1207. #if HTS_OVERRIDE_DOS_FOLDERS
  1208.   /* Replace /foo/nul/bar by /foo/nul_/bar */
  1209.   {
  1210.     int i=0;
  1211.     while(hts_tbdev[i][0]) {
  1212.       char* a=save;
  1213.       while((a=strstrcase(a,(char*)hts_tbdev[i]))) {
  1214.         switch ( (int) a[strlen(hts_tbdev[i])] ) {
  1215.         case '\0':
  1216.         case '/':
  1217.         case '.': 
  1218.         {
  1219.           char BIGSTK tempo[HTS_URLMAXSIZE*2]; tempo[0]='\0';
  1220.           strncatbuff(tempo,save,(int) (a - save) + strlen(hts_tbdev[i]));
  1221.           strcatbuff(tempo,"_");
  1222.           strcatbuff(tempo,a+strlen(hts_tbdev[i]));
  1223.           strcpybuff(save,tempo);
  1224.                    }
  1225.           break;
  1226.         }
  1227.         a+=strlen(hts_tbdev[i]);
  1228.       }
  1229.       i++;
  1230.     }
  1231.   }
  1232.   /* Strip ending . or ' ' forbidden on windoz */
  1233.   {
  1234.     int len;
  1235.     char* a=save;
  1236.     while((a=strstr(a,"./"))) {
  1237.       *a = '_';
  1238.     }
  1239.     a=save;
  1240.     while((a=strstr(a," /"))) {
  1241.       *a = '_';
  1242.     }
  1243.     len = (int) strlen(save);
  1244.     if (len > 0 && ( save[len - 1] == '.' || save[len - 1] == ' ') ) {
  1245.       save[len - 1] = '_';
  1246.     }
  1247.   }
  1248. #endif
  1249.  
  1250.   // conversion 8-3 .. y compris pour les rΘpertoires
  1251.   if (opt->savename_83) {
  1252.     char BIGSTK n83[HTS_URLMAXSIZE*2];
  1253.     long_to_83(opt->savename_83,n83,save);
  1254.     strcpybuff(save,n83);
  1255.   }
  1256.  
  1257.   // enforce stricter ISO9660 compliance (bug reported by Steffo Carlsson)
  1258.   // Level 1 File names are restricted to 8 characters with a 3 character extension, 
  1259.   // upper case letters, numbers and underscore; maximum depth of directories is 8.
  1260.   // This will be our "DOS mode"
  1261.   // L2: 31 characters
  1262.   // A-Z,0-9,_
  1263.   if (opt->savename_83 > 0) {
  1264.     char *a, *last;
  1265.     for(last = save + strlen(save) - 1 ; last != save && *last != '/' && *last != '\\' && *last != '.' ; last--);
  1266.     if (*last != '.') {
  1267.       last = NULL;
  1268.     }
  1269.     for(a = save ; *a != '\0' ; a++) {
  1270.       if (*a >= 'a' && *a <= 'z') {
  1271.         *a -= 'a' - 'A';
  1272.       }
  1273.       else if (*a == '.') {
  1274.         if (a != last) {
  1275.           *a = '_';
  1276.         }
  1277.       }
  1278.       else if ( ! ( (*a >= 'A' && *a <= 'Z') || (*a >= '0' && *a <= '9') || *a == '_' || *a == '/' || *a == '\\') ) {
  1279.         *a = '_';
  1280.       }
  1281.     }
  1282.   }
  1283.  
  1284.   /* ensure that there is no ../ (potential vulnerability) */
  1285.   fil_simplifie(save);
  1286.  
  1287.     /* callback */
  1288.   RUN_CALLBACK5(opt, savename, adr_complete,fil_complete,referer_adr,referer_fil,save);
  1289.  
  1290.   if ( (opt->debug>0) && (opt->log!=NULL) ) {
  1291.         HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: save-name: local name: %s%s -> %s"LF,adr,fil,save);
  1292.         test_flush;
  1293.     }
  1294.  
  1295.     /* Ensure that the MANDATORY "temporary" extension is set */
  1296.     if (ext_chg_delayed) {
  1297.         char *ptr;
  1298.         char *lastDot = NULL;
  1299.         for(ptr = save ; *ptr != 0 ; ptr++) {
  1300.             if (*ptr == '.') {
  1301.                 lastDot = ptr;
  1302.             } else if (*ptr == '/' || *ptr == '\\') {
  1303.                 lastDot = NULL;
  1304.             }
  1305.         }
  1306.         if (lastDot == NULL) {
  1307.             strcatbuff(save, "." DELAYED_EXT);
  1308.         } else if (!IS_DELAYED_EXT(save)) {
  1309.             strcatbuff(lastDot, "." DELAYED_EXT);
  1310.         }
  1311.     }
  1312.  
  1313.   // chemin primaire Θventuel A METTRE AVANT
  1314.   if (strnotempty(StringBuff(opt->path_html))) {
  1315.     char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1316.     strcpybuff(tempo,StringBuff(opt->path_html));
  1317.     strcatbuff(tempo,save);
  1318.     strcpybuff(save,tempo);
  1319.   }
  1320.  
  1321.  
  1322.   // vΘrifier que le nom n'est pas dΘja pris...
  1323.   if (liens!=NULL) { 
  1324.     int nom_ok;
  1325.     do {
  1326.       int i;
  1327.       //
  1328.       nom_ok=1;  // α priori bon
  1329.       // on part de la fin pour optimiser, plus les opti de taille pour aller encore plus vite..
  1330. #if DEBUG_SAVENAME
  1331. printf("\nStart search\n");
  1332. #endif
  1333.  
  1334.       i=hash_read(hash,save,"",0,0);      // lecture type 0 (sav)
  1335.       if (i>=0)
  1336.           {
  1337.             int sameAdr = ( strfield2(liens[i]->adr, normadr) != 0 );
  1338.             int sameFil;
  1339.                         // NO - URL hack is only for stripping // and www.
  1340.             //if (opt->urlhack != 0)
  1341.             //  sameFil = ( strfield2(liens[i]->fil, normfil) != 0);
  1342.             //else
  1343.             sameFil = ( strcmp(liens[i]->fil, normfil) == 0);
  1344.             if (sameAdr && sameFil)
  1345.             {    // ok c'est le mΩme lien, adresse dΘja dΘfinie
  1346.               /* Take the existing name not to screw up with cAsE sEnSiTiViTy of Linux/Unix */
  1347.               if (strcmp(liens[i]->sav, save) != 0) {
  1348.                 strcpybuff(save, liens[i]->sav);
  1349.               }
  1350.               i=0;
  1351. #if DEBUG_SAVENAME
  1352. printf("\nOK ALREADY DEFINED\n",13,i);
  1353. #endif
  1354.             } else {  // utilisΘ par un AUTRE, changer de nom
  1355.               char BIGSTK tempo[HTS_URLMAXSIZE*2];
  1356.               char* a=save+strlen(save)-1;
  1357.               char* b;
  1358.               int n=2;       
  1359.               char collisionSeparator = ( (opt->savename_83 != 2) ? '-' : '_');
  1360.               tempo[0]='\0';
  1361.  
  1362. #if DEBUG_SAVENAME
  1363. printf("\nWRONG CASE UNMATCH : \n%s\n%s, REDEFINE\n",liens[i]->fil,fil_complete);
  1364. #endif
  1365.               nom_ok=0;
  1366.               i=0;
  1367.               
  1368.               while(( a > save) && (*a!='.') && (*a!='\\') && (*a!='/')) a--;
  1369.               if (*a=='.')
  1370.                 strncatbuff(tempo,save,(int) (a - save));
  1371.               else
  1372.                 strcatbuff(tempo,save);
  1373.               
  1374.               // tester la prΘsence d'un -xx (ex: index-2.html -> index-3.html)
  1375.               b=tempo+strlen(tempo)-1;
  1376.               while (isdigit((unsigned char)*b)) b--;
  1377.               if (*b == collisionSeparator) {
  1378.                 sscanf(b+1,"%d",&n);
  1379.                 *b='\0';    // couper
  1380.                 n++;  // plus un
  1381.               }
  1382.               
  1383.               // en plus il faut gΘrer le 8-3 .. pas facile le client
  1384.               if (opt->savename_83) {
  1385.                 int max;
  1386.                 char* a=tempo+strlen(tempo)-1;
  1387.                 while(( a > tempo) && (*a!='/')) a--;
  1388.                 if (*a=='/') a++;
  1389.                 max=max_char-1-nombre_digit(n);
  1390.                 if ((int) strlen(a)>max)
  1391.                   *(a+max)='\0';  // couper sinon il n'y aura pas la place!
  1392.               }
  1393.               
  1394.               // ajouter -xx (ex: index.html -> index-2.html)
  1395.               sprintf(tempo+strlen(tempo),"%c%d", collisionSeparator, n);
  1396.               
  1397.               // ajouter extension
  1398.               if (*a=='.')
  1399.                 strcatbuff(tempo,a);
  1400.               
  1401.               strcpybuff(save,tempo);
  1402.               
  1403.               //printf("switched: %s\n",save);
  1404.               
  1405.             }  // if
  1406.           }
  1407. #if DEBUG_SAVENAME
  1408. printf("\nEnd search, %s\n",fil_complete);
  1409. #endif
  1410.     } while(!nom_ok);
  1411.     
  1412.   }
  1413.   
  1414.   //printf("'%s' %s %s\n",save,adr,fil);
  1415.       
  1416.   return 0;
  1417. }
  1418.  
  1419. /* nom avec md5 urilisΘ partout */
  1420. void standard_name(char* b,char* dot_pos,char* nom_pos,char* fil_complete,int short_ver) {
  1421.     char md5[32 + 2];
  1422.  
  1423.   b[0]='\0';
  1424.   /* Nom */
  1425.   if (dot_pos) {
  1426.     if (!short_ver)    // Noms longs
  1427.       strncatbuff(b,nom_pos,(int) (dot_pos - nom_pos));
  1428.     else
  1429.       strncatbuff(b,nom_pos,min((int) (dot_pos - nom_pos),8));
  1430.   } else {
  1431.     if (!short_ver)    // Noms longs
  1432.       strcatbuff(b,nom_pos);
  1433.     else
  1434.       strncatbuff(b,nom_pos,8);
  1435.   }
  1436.   /* MD5 - 16 bits */
  1437.   strncatbuff(b,url_md5(md5,fil_complete),4);
  1438.   /* Ext */
  1439.   if (dot_pos) {
  1440.     strcatbuff(b,".");
  1441.     if (!short_ver)    // Noms longs
  1442.       strcatbuff(b,dot_pos+1);
  1443.     else
  1444.       strncatbuff(b,dot_pos+1,3);
  1445.   } else {
  1446.     if (!short_ver)    // Noms longs
  1447.       strcatbuff(b,DEFAULT_EXT + 1);    // pas de..
  1448.     else
  1449.       strcatbuff(b,DEFAULT_EXT_SHORT + 1);    // pas de..
  1450.   }
  1451. }
  1452.  
  1453.  
  1454. /* Petit md5 */
  1455. char* url_md5(char* digest, char* fil_complete) {
  1456.   char* a;
  1457.   digest[0]='\0';
  1458.   a=strchr(fil_complete,'?');
  1459.   if (a) {
  1460.     if (strlen(a)) {
  1461.       char BIGSTK buff[HTS_URLMAXSIZE*2];
  1462.       a++;
  1463.       digest[0]=buff[0]='\0';
  1464.       strcatbuff(buff,a);         /* query string MD5 */
  1465.       domd5mem(buff,strlen(buff),digest,1);
  1466.     }
  1467.   }
  1468.   return digest;
  1469. }
  1470.  
  1471. // interne α url_savename: ajoute une chaεne α une autre avec \ -> /
  1472. void url_savename_addstr(char* d,char* s) {
  1473.   int i = (int) strlen(d);
  1474.   while(*s) {
  1475.     if (*s=='\\')  // remplacer \ par des /
  1476.       d[i++]='/';
  1477.     else
  1478.       d[i++]=*s;
  1479.     s++;
  1480.   }
  1481.   d[i]='\0';
  1482. }
  1483.  
  1484. #undef test_flush
  1485.